package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

public class ClientUI extends JFrame implements Runnable{

	Socket socket;
	BufferedWriter send = null;
	BufferedReader receive =null;
	boolean threadFlag = false;
	StringBuilder data = new StringBuilder();
	String choice;
	String youCh;
	String youface;
	
	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JTextField ip;
	private JLabel lblNewLabel_1;
	private JTextField id;
	private JButton bt;
	private JButton bt1;
	private JLabel lblNewLabel_2;
	private JTextField textField;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_5;
	private JLabel lblNewLabel_6;
	private JButton bt2;
	private JButton bt3;
	private JButton bt4;
	private JLabel lblNewLabel_7;
	private JLabel lblNewLabel_8;
	private JLabel lblNewLabel_9;
	private JLabel lblNewLabel_10;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientUI frame = new ClientUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 630, 431);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getIp());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getId());
		contentPane.add(getBt());
		contentPane.add(getBt1());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getTextField());
		contentPane.add(getLblNewLabel_3());
		contentPane.add(getLblNewLabel_4());
		contentPane.add(getLblNewLabel_5());
		contentPane.add(getLblNewLabel_6());
		contentPane.add(getBt2());
		contentPane.add(getBt3());
		contentPane.add(getBt4());
		contentPane.add(getLblNewLabel_7());
		contentPane.add(getLblNewLabel_8());
		contentPane.add(getLblNewLabel_9());
		contentPane.add(getLblNewLabel_10());
		
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("서버: IP" + ia.getHostAddress() + ", port: " + "4444");

		} catch (Exception e) {

		}
		
	}
	
	public void serverStart() {
		try {
			Thread t = new Thread(this);
			t.start();
			
			bt.setEnabled(false);
			bt1.setEnabled(true);
			bt2.setEnabled(true);
			bt3.setEnabled(true);
			bt4.setEnabled(true);
			
			
			dataSet("<b>서버가 시작 되었네 그려~ </b>");	
		} catch (Exception e) {

		}
	}

	public void serverStop() {
		bt.setEnabled(true);
		bt1.setEnabled(false);
		bt2.setEnabled(true);
		bt3.setEnabled(true);
		bt4.setEnabled(true);
		
		threadFlag = false; 
		
		try {

		} catch (Exception e) {

		}

	}

	public void dataSet(String d) {
		try {
			data.append(d+"<br/>");
			//result.setText(data.toString());

		} catch (Exception e) {

		}

	}
	
	public void sendMsg(String ms){
		try{
			send.write(ms);
			send.newLine();
			send.flush();
		//	dataSet("<font color = 'blue'> 나 :"+ms+"</font>");
		} catch (Exception ex){
			ex.printStackTrace();
		 }
	}
	
	public void run() {
		try {
			socket = new Socket(ip.getText(),4444);
			
			OutputStream os = socket.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			send = new BufferedWriter(osw);
			
			InputStream is = socket.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			receive = new BufferedReader(isr);

			threadFlag = true;
			
			sendMsg("145");
			
			
			while(threadFlag){
				String str = receive.readLine();
				dataSet("수신: " + str);
				
				youface =str;
				if(str.equals("1")){
					lblNewLabel_6.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\1.bmp"));
				}
				youCh =str;
				if(str.equals("가위")){
					lblNewLabel_10.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\가위.jpg"));
				}else if(str.equals("바위")){
					lblNewLabel_10.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\바위.png"));
				}else if(str.equals("보")){
					lblNewLabel_10.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\보.jpg"));
					
				}
				
				
			}
			
			dataSet("<font color = 'red' > 서버가 중지 되었네 그려~ </font>");
			bt.setEnabled(true);
			bt1.setEnabled(false);
			
			receive.close();
			isr.close();
			is.close();
			
			send.close();
			osw.close();
			os.close();
			
			socket.close();
			
		} catch (Exception e) {

		}

	}
	
	

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("서버 IP");
			lblNewLabel.setBounds(12, 24, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getIp() {
		if (ip == null) {
			ip = new JTextField();
			ip.setText("192.168.10.130");
			ip.setBounds(79, 19, 116, 26);
			ip.setColumns(10);
		}
		return ip;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("유저 ID");
			lblNewLabel_1.setBounds(207, 24, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getId() {
		if (id == null) {
			id = new JTextField();
			id.setText("주인님");
			id.setBounds(276, 19, 103, 26);
			id.setColumns(10);
		}
		return id;
	}
	private JButton getBt() {
		if (bt == null) {
			bt = new JButton("접속");
			bt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStart();
				}
			});
			bt.setBounds(407, 20, 97, 23);
		}
		return bt;
	}
	private JButton getBt1() {
		if (bt1 == null) {
			bt1 = new JButton("종료");
			bt1.setEnabled(false);
			bt1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStop();
				}
			});
			bt1.setBounds(516, 20, 97, 23);
		}
		return bt1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("게임 전적 :");
			lblNewLabel_2.setBounds(194, 62, 70, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setText("  전  승  패  무");
			textField.setHorizontalAlignment(SwingConstants.CENTER);
			textField.setBounds(265, 55, 200, 26);
			textField.setColumns(10);
		}
		return textField;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("내 이미지");
			lblNewLabel_3.setBounds(41, 97, 57, 15);
		}
		return lblNewLabel_3;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("");
			lblNewLabel_4.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_4.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\145.bmp"));
			lblNewLabel_4.setBounds(12, 122, 116, 90);
		}
		return lblNewLabel_4;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("상대 이미지");
			lblNewLabel_5.setBounds(39, 222, 70, 15);
		}
		return lblNewLabel_5;
	}
	private JLabel getLblNewLabel_6() {
		if (lblNewLabel_6 == null) {
			lblNewLabel_6 = new JLabel("");
			lblNewLabel_6.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_6.setBounds(12, 247, 116, 90);
		}
		return lblNewLabel_6;
	}
	private JButton getBt2() {
		if (bt2 == null) {
			bt2 = new JButton("가위");
			bt2.setEnabled(false);
			bt2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					lblNewLabel_9.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\가위.jpg"));
					sendMsg("가위");
					bt3.setEnabled(false);
					bt4.setEnabled(false);
				}
			});
			bt2.setBounds(12, 346, 116, 23);
		}
		return bt2;
	}
	private JButton getBt3() {
		if (bt3 == null) {
			bt3 = new JButton("바위");
			bt3.setEnabled(false);
			bt3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					lblNewLabel_9.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\바위.png"));
					sendMsg("바위");
					bt2.setEnabled(false);
					bt4.setEnabled(false);
				}
			});
			bt3.setBounds(207, 346, 116, 23);
		}
		return bt3;
	}
	private JButton getBt4() {
		if (bt4 == null) {
			bt4 = new JButton("보");
			bt4.setEnabled(false);
			bt4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					lblNewLabel_9.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\보.jpg"));
					sendMsg("보");
					bt2.setEnabled(false);
					bt3.setEnabled(false);
				}
			});
			bt4.setBounds(407, 346, 116, 23);
		}
		return bt4;
	}
	private JLabel getLblNewLabel_7() {
		if (lblNewLabel_7 == null) {
			lblNewLabel_7 = new JLabel("나");
			lblNewLabel_7.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_7.setBounds(207, 97, 57, 15);
		}
		return lblNewLabel_7;
	}
	private JLabel getLblNewLabel_8() {
		if (lblNewLabel_8 == null) {
			lblNewLabel_8 = new JLabel("접속자");
			lblNewLabel_8.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_8.setBounds(441, 97, 57, 15);
		}
		return lblNewLabel_8;
	}
	private JLabel getLblNewLabel_9() {
		if (lblNewLabel_9 == null) {
			lblNewLabel_9 = new JLabel("");
			lblNewLabel_9.setBounds(138, 122, 200, 174);
		}
		return lblNewLabel_9;
	}
	private JLabel getLblNewLabel_10() {
		if (lblNewLabel_10 == null) {
			lblNewLabel_10 = new JLabel("");
			lblNewLabel_10.setBounds(366, 122, 200, 174);
		}
		return lblNewLabel_10;
	}
}
